package com.letwal.cmcvnd.create

import android.annotation.SuppressLint
import android.view.View
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.letwal.cmcvnd.R
import com.letwal.cmcvnd.adata.RevDataMnemonic
import com.letwal.cmcvnd.adata.login.ReqLogin
import com.letwal.cmcvnd.aviewmodel.LoginViewModel
import com.letwal.cmcvnd.base.BaseActivity
import com.letwal.cmcvnd.create.adapter.DataMnemonicVerify
import com.letwal.cmcvnd.create.adapter.MnemonicVerifyAdapter
import com.letwal.cmcvnd.databinding.ActivityCreateWalletVerifyBinding
import com.letwal.cmcvnd.enums.PageTagEnum
import com.letwal.cmcvnd.extend.*
import java.util.*

/** 创建钱包-验证页 */
@SuppressLint("CustomSplashScreen")
class CreateWalletVerifyActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivityCreateWalletVerifyBinding

    //适配器
    private lateinit var mMnemonicVerifyAdapter: MnemonicVerifyAdapter

    //登录viewModel
    private val mLoginViewModel by lazy {
        ViewModelProvider(this)[LoginViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mLoginViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.create_wallet_verify.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding =
            ActivityCreateWalletVerifyBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    override fun onContentViewBinded() {
        try {
            //监听live data 返回
            onHandleLiveDataCallback()

            //初始化适配器
            val zjc = intent.getStringExtra("zjc").toString()
            val list = zjc.toStringList()

            val allList = mutableListOf<RevDataMnemonic>()
            for (index in list.indices) {
                allList.add(RevDataMnemonic(index = (index + 1), content = list[index]))
            }

            val random = Random()
            //生成3个随机数
            val num1 = random.nextInt(allList.size)
            var num2 = random.nextInt(allList.size)
            while (num1 == num2) {
                num2 = random.nextInt(allList.size)
            }
            var num3 = random.nextInt(allList.size)
            while ((num1 == num3) or (num2 == num3)) {
                num3 = random.nextInt(allList.size)
            }
            println("=====3个随机数：$num1 $num2 $num3")

            //筛选剩下的项
            val allListTemp = mutableListOf<RevDataMnemonic>()
            for (index in 0 until allList.size) {
                val isNum1 = (num1 == index)
                val isNum2 = (num2 == index)
                val isNum3 = (num3 == index)

                if ((!isNum1) and (!isNum2) and (!isNum3)) {
                    allListTemp.add(allList[index])
                }
            }

            //从剩下的项选出9个值
            val itme1 = random.nextInt(allListTemp.size)
            var itme2 = random.nextInt(allListTemp.size)
            while (itme1 == itme2) {
                itme2 = random.nextInt(allListTemp.size)
            }

            var itme3 = random.nextInt(allListTemp.size)
            while ((itme1 == itme3) or (itme2 == itme3)) {
                itme3 = random.nextInt(allListTemp.size)
            }

            var itme4 = random.nextInt(allListTemp.size)
            while ((itme1 == itme4) or (itme2 == itme4) or (itme3 == itme4)) {
                itme4 = random.nextInt(allListTemp.size)
            }

            var itme5 = random.nextInt(allListTemp.size)
            while ((itme1 == itme5) or (itme2 == itme5) or (itme3 == itme5) or (itme4 == itme5)) {
                itme5 = random.nextInt(allListTemp.size)
            }

            var itme6 = random.nextInt(allListTemp.size)
            while ((itme1 == itme6) or (itme2 == itme6) or (itme3 == itme6) or (itme4 == itme6) or (itme5 == itme6)) {
                itme6 = random.nextInt(allListTemp.size)
            }

            //组装3行
            val resultList = mutableListOf<DataMnemonicVerify>()

            val row1Real = random.nextInt(3)
            val row1 = mutableListOf<RevDataMnemonic>()
            row1.add(allListTemp[itme1])
            row1.add(allListTemp[itme2])
            row1.add(row1Real, allList[num1])
            resultList.add(DataMnemonicVerify(index = num1+1, list = row1))

            val row2Real = random.nextInt(3)
            val row2 = mutableListOf<RevDataMnemonic>()
            row2.add(allListTemp[itme3])
            row2.add(allListTemp[itme4])
            row2.add(row2Real, allList[num2])
            resultList.add(DataMnemonicVerify(index = num2+1, list = row2))

            val row3Real = random.nextInt(3)
            val row3 = mutableListOf<RevDataMnemonic>()
            row3.add(allListTemp[itme5])
            row3.add(allListTemp[itme6])
            row3.add(row3Real, allList[num3])
            resultList.add(DataMnemonicVerify(index = num3+1, list = row3))

            //初始化
            mMnemonicVerifyAdapter = MnemonicVerifyAdapter(this, resultList)

            mActivityBinding.listCreateWalletVerify.layoutManager = LinearLayoutManager(this)
            mActivityBinding.listCreateWalletVerify.adapter = mMnemonicVerifyAdapter

            //点击确定
            mActivityBinding.submitCreateWalletVerify.setOnClickListener {
                //判断是否选对
                val hasSuccess = resultList.all { item ->
                    (item.hadCheckedSuccess())
                }
                if (hasSuccess) {
                    //更新UI
                    setVerifySuccess(true)

                    //去登录
                    //初始化配置参数
                    val email = intent.getStringExtra("email").toString()
                    val password = intent.getStringExtra("password").toString()
                    val zjcList = mutableListOf<String>()
                    resultList.forEach { resultItem ->
                        val checkContent = resultItem.checkedSuccessContent()
                        if (checkContent.isNotBlank()) {
                            zjcList.add(checkContent)
                        }
                    }
                    mLoginViewModel.login(
                        ReqLogin(
                            email = email,
                            password = password,
                            zjc = zjc//zjcList.joinToString(",")
                        )
                    )
                } else {
                    Toast.makeText(
                        this,
                        getString(R.string.create_wallet_err_tips1),
                        Toast.LENGTH_SHORT
                    ).show()
                }
            }

            //更新UI
            setVerifySuccess(false)

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    /** 监听live data 返回 */
    private fun onHandleLiveDataCallback() {
        //登录回调
        mLoginViewModel.onLoginLiveData.observe(this) {
            if (it.isSuccess) {
                startToHomeActivity()
            }
        }
    }

    /** 更新提交UI */
    private fun setVerifySuccess(success: Boolean) {
        if (success) {
            mActivityBinding.submitCreateWalletVerify.visibility = View.GONE
            mActivityBinding.llCreateWalletVerifySuccess.visibility = View.VISIBLE
        } else {
            mActivityBinding.submitCreateWalletVerify.visibility = View.VISIBLE
            mActivityBinding.llCreateWalletVerifySuccess.visibility = View.GONE
        }
    }
}